// pah
#include "pah800x_bsp.h"


//============================================================================
// Definition
//============================================================================
//#define PAH800xx_I2C_SLAVE_ID 0x40
//#define I2C_PPG_PORT_ID 1


//============================================================================
// Public function
//============================================================================
void pah_delay_ms(uint32_t ms)
{
    //pxi_nrf_delay_ms(ms);//zhao
    HR_OS_DELAY_MS(ms);
}

uint64_t pah_get_tick_count(void)
{
    //return pxi_nrf_get_tick_count();//zhao
    return em_rtc_get_ticks()/32;
}

pah_ret pah_comm_i2c_write(uint8_t addr, const uint8_t *data, size_t write_size)
{
    ////DEBUG_PRINT("W A:0x%02x,D:0x%02x\n",addr,*data);
    //pxi_nrf_i2c_write(addr, data, write_size);//zhao
    
	HRS_SET_SLAVE_ADDR( PAH8009_SLAVE_ADDR_7BIT );
    uint8_t w[18];
    hrs_op_data_t* write = (hrs_op_data_t*)w;
    write->reg = addr;
    if(write_size > 17)
        write_size = 17;
    for(int i = 0; i < write_size; i ++ ) 
    {
        write->data[i] = data[i];
    }
    uint32_t ret = HR_I2C_WRITE( PAH8009_SLAVE_ADDR_7BIT, &write->reg, write->data, write_size);
    if( ret != HR_I2C_SUCCESS )
    {
        LOG_PRINT("pah8009 write fail, reason = 0x%X\r\n", ret ); 
        return PAH_RET_FAILED; 
    }
    
    return PAH_RET_SUCCESS;
}

pah_ret pah_comm_i2c_read(uint8_t addr, uint8_t *data, size_t read_size)
{
    //pxi_nrf_i2c_read(addr, data, read_size);//zhao
	HRS_SET_SLAVE_ADDR( PAH8009_SLAVE_ADDR_7BIT );
    uint8_t w[18];
    hrs_op_data_t* write = (hrs_op_data_t*)w;
    write->reg = addr;
    uint32_t ret = HR_I2C_WRITE( PAH8009_SLAVE_ADDR_7BIT, &write->reg, write->data, 0);
    if( ret != HR_I2C_SUCCESS )
    {
        LOG_PRINT("pah8009 w fail in read, reason = 0x%X\r\n", ret );
        return PAH_RET_FAILED; 
    }
	platform_delay_us(1);
    if( HR_I2C_READ( PAH8009_SLAVE_ADDR_7BIT,data, read_size) != HR_I2C_SUCCESS )
	{
        LOG_PRINT("pah8009 read fail, reason = 0x%X\r\n", ret );
	 	return PAH_RET_FAILED;
	}

    return PAH_RET_SUCCESS;
}

void pah_print(
    int32_t                         fileline,
    const char                      *funcname,
    const char                      *filename,
    const char                      *format, ...)
{
    UNUSED_VAR(fileline);
    UNUSED_VAR(funcname);
    //DBG_DIRECT("func:%s",funcname);
    UNUSED_VAR(filename);

    static char dest[256];
    va_list argptr;
    int char_num = 0;

    if (!format)
        return;

    va_start(argptr, format);
    char_num = vsprintf(dest, format, argptr);
    va_end(argptr);

    //uart_printf("%s", dest);

    dest[char_num] = '\0';
    
    
    ////pxi_nrf_uart_transmit((uint8_t*)dest, (uint16_t)(char_num + 1));//zhao
    //LOG1("[PAH8009]:%b", TRACE_STRING(dest));
}

void ReadGSensor(float *x, float *y, float *z)
{
    
}
